﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NthDownload.Common.ViewModels;
using NthDownload.Common.Mapping;
using NthDownload.Contracts;

namespace NthDownload.DownloadPlugins.Http.ViewModels
{
    public class HttpCredentialsSettingsViewModel : ViewModelScreenBase
    {
        #region Class Members

        private HttpSettings _HttpSettings;
        private IList _selectedHttpCredentials;

        #endregion Class Members

        #region Constructors

        public HttpCredentialsSettingsViewModel(HttpSettings settings)
        {
            Settings = settings;
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// The currently selected basic http credentials
        /// </summary>
        public IList SelectedHttpCredentials
        {
            get
            {
                return _selectedHttpCredentials;
            }
            set
            {
                _selectedHttpCredentials = value;
                NotifyOfPropertyChange(() => SelectedHttpCredentials);
            }
        }

        /// <summary>
        /// Settings
        /// </summary>
        public HttpSettings Settings 
        {
            get
            {
                return _HttpSettings;
            }
            private set
            {
                _HttpSettings = value;
                NotifyOfPropertyChange(() => Settings);
            }
        }

        #endregion Properties

        #region Methods

        //
        // Adds new credentials
        //
        public void AddCredentials()
        {
            HttpCredentials credentials = new HttpCredentials();

            HttpCredentialsDetailsViewModel HttpCredentialsDetailsViewModel = new HttpCredentialsDetailsViewModel(credentials);

            if (NavigationService.ShowDialog(HttpCredentialsDetailsViewModel, false) == ViewModelResults.Save)
            {
                Settings.Credentials.Add(credentials);
            }
        }

        //
        // Displays the details screen for the given credentials
        //
        public void EditCredentials(HttpCredentials credentials)
        {
            HttpCredentials clonedHttpCredentials = (HttpCredentials)ObjectMappingService.Clone(credentials);

            HttpCredentialsDetailsViewModel HttpCredentialsDetailsViewModel = new HttpCredentialsDetailsViewModel(clonedHttpCredentials);

            if (NavigationService.ShowDialog(HttpCredentialsDetailsViewModel, false) == ViewModelResults.Save)
            {
                ObjectMappingService.Update(clonedHttpCredentials, credentials);
            }
        }

        //
        // Removes the selected credentials
        //
        public void RemoveCredentials(HttpCredentials credentials)
        {
            //TODO add delete confirmation

            //
            // If the item is selected then remove all selected items otherwise only removed the passed item
            //
            if (SelectedHttpCredentials != null && SelectedHttpCredentials.Contains(credentials))
            {
                List<HttpCredentials> selectedHttpCredentials = SelectedHttpCredentials.OfType<HttpCredentials>().ToList();
                foreach (HttpCredentials item in selectedHttpCredentials)
                {
                    Settings.Credentials.Remove(item);
                }
            }
            else
            {
                Settings.Credentials.Remove(credentials);
            }
        }

        #endregion Methods
    }
}
